<?php

require_once "Zend/Tokenizer/Interface.php";

/**
 * @category   Zend
 * @package    Zend_Tokenizer
 * @copyright  Salomon BRYS
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 *
 * This tokenizer gives tokens representing a line from the stream.
 */
class Zend_Tokenizer_String_Producer implements Zend_Tokenizer_Interface
{
	/**
	 * @var string
	 */
	private $_data;

	/**
	 * @var bool
	 */
	private $_closed = false;

	/**
	 * Add the buffered data not tokenized yet.
	 * Used to transfer data when tokenizer is changing on the fly.
	 *
	 * @return string
	 */
	public function getRawData()
	{
		return $this->_data;
	}

	/**
	 * return wether there's at least one token ready.
	 *
	 * @return bool
	 */
	public function hasToken()
	{
		return !empty($this->_data);
	}

	/**
	 * Return the next token.
	 *
	 * @return mixed
	 */
	public function getToken()
	{
		if (empty($this->_data)) {
			return null;
		}
		$ret = $this->_data;
		$this->_data = "";
		return $ret;
	}

	/**
	 * Closes the stream.
	 * Announce the end of it.
	 */
	public function close()
	{
		$this->_closed = true;
	}

	/**
	 * Add some data to the token buffer.
	 *
	 * @param string $data
	 */
	public function addData($data)
	{
		if ($this->_closed)
			return ;
		$this->_data .= $data;
	}

	/**
	 * Return wether it is the of the stream.
	 *
	 * @return bool
	 */
	public function isEndOfStream()
	{
		return empty($this->_data) && $this->_closed;
	}

	/**
	 * Return wether the tokenizer produces string tokens
	 *
	 * @return bool
	 */
	public function isStringToken()
	{
		return true;
	}

	/**
	 * Get the 'first' tokenizer. The producer.
	 * If a decorator, returns the decorated tokenizer.
	 * If a producer, returns itself.
	 *
	 * @return Zend_Tokenizer_Interface
	 */
	public function getProducer()
	{
		return $this;
	}
}
